package com.mtask.web.controller;

import com.mtask.common.entity.task.TaskJob;
import com.mtask.task.service.TaskService;
import com.mtask.common.dto.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/task")
@RestController
@ResponseBody
public class TaskController {
    @Resource
    private TaskService taskService;

    @GetMapping("start/all")
    public List<TaskJob> startAll() {
        return taskService.startAllJobWithDb();
    }

    @GetMapping("delete")
    public List<TaskJob> deleteAll() {
        return taskService.deleteAllJobWithDb();
    }

    @GetMapping("resume/{id}")
    public Result<String> resume(@PathVariable Integer id) {
        if (taskService.startJob(id)) {
            return new Result<>(200, "ok", "resumed");
        }
        return new Result<>(201, "ok", "resume failed");
    }

    @GetMapping("stop/{id}")
    public Result<String> stop(@PathVariable Integer id) {
        if (taskService.stopJob(id)) {
            return new Result<>(200, "ok", "stopped");
        }
        return new Result<>(201, "ok", "stop failed");
    }
}
